package com.lagou.zk.controller;

import com.lagou.zk.utils.JDBCUtil;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ZookeeperController {

    @Value("${zkurl}")
    private String zkurl;
    @Value("${mysqlNode}")
    private String mysqlNode;

    @RequestMapping("/updateMysqlUrl")
    public String updateMysqlUrl(@RequestParam("ipAndPort") String ipAndPort, @RequestParam("databaseName") String databaseName
            , Model model){
        model.addAttribute("msg","创建了新的数据库连接池：  " + JDBCUtil.dataSource.toString());
        update(ipAndPort,databaseName);
        return "index";
    }

    // 就不写服务层了
    public void update(String ipAndPort,String databaseName){ // http://localhost:8080/updateMysqlUrl?ipAndPort=hadoop01:3306&databaseName=hue
        ZkClient zkClient = new ZkClient(zkurl);
        zkClient.writeData(mysqlNode,"jdbc:mysql://"+ipAndPort+"/"+databaseName+"?useUnicode=true&characterEncoding=utf8&useSSL=true");
    }
}
